package com.octopus.yangj.customized.rule;

import com.octopus.kernal.controller.SearchOperator;
import com.octopus.kernal.model.BaseStruct;
import com.octopus.kernal.service.BusinessRuleInterface;
import com.octopus.yangj.generated.controller.SearchEventExecuteCommand;
import com.octopus.yangj.generated.model.EventExecuteStruct;
import com.octopus.yangj.generated.model.ExecuteUnitStruct;
import com.octopus.yangj.generated.service.EventExecuteService;
import com.octopus.yangj.util.ServiceFactory;

public class ExecuteUnitDeleteRule implements BusinessRuleInterface{

	@Override
	public boolean isAllowed(BaseStruct struct) {
		ExecuteUnitStruct unit = (ExecuteUnitStruct)struct;
		SearchEventExecuteCommand searchCommand = new SearchEventExecuteCommand();
		searchCommand.setEntity(new EventExecuteStruct());
		searchCommand.setOperator(new EventExecuteStruct());
		searchCommand.getEntity().setExecuteUnitId(unit.getId());
		searchCommand.getOperator().setExecuteUnitId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
		EventExecuteService eventExecuteService = ServiceFactory.getEventExecuteService();
		int count = eventExecuteService.searchCount(searchCommand);
		if(count > 0){
			return false;
		}else{
			return true;
		}
	}

}
